/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlers;

import gestionnaires.GestionFilm;
import gestionnaires.GestionUtilisateur;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modeles.Utilisateur;

/**
 *
 * @author mazid
 */

@WebServlet(name = "ServletSession", urlPatterns = {"/session"})
public class ServletSession extends HttpServlet {
    @EJB
    private GestionFilm gestionFilm;
    @EJB
    private GestionUtilisateur gestionUtilisateur;
    

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String action =request.getParameter("action");
        String page = request.getHeader("Referer");
        HttpSession session = request.getSession();
        String login = request.getParameter("login");
        String pwd = request.getParameter("motDePasse");
        Utilisateur users =null;
        String forwardTo = "index.jsp?";
        
        // on test si l'action n'est pas egal a null
        if(action!=null){
            
         if(action.equals("testAuthen")){
            //on test si le login et mot de passe est correcte
            users = gestionUtilisateur.testLoginUsers(login, pwd);
            
            //si le login et password est correcte
            if(users!=null){
                
                session.setAttribute("connecte", users);  
                
            }
            else{
               //par mesure de securité on met connecte à null et on renvoie un message d'erreur
                session.setAttribute("connecte",null);
                                              
            }
        }
        //action pour se deconnecter 
        if(action.equals("deconnecter")){

            session.setAttribute("connecte", null);                                                      
        }
        
       }
//        RequestDispatcher dp = request.getRequestDispatcher("/ServletFilm?action=index");
//        dp.forward(request, response);
        request.setAttribute("login", login);
        response.sendRedirect(page);
      }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

   
}
